package gdut.jmxywz.action;

import gdut.jmxywz.been.Info;
import gdut.jmxywz.been.InfoFiles;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.apache.struts2.ServletActionContext;

@SuppressWarnings("serial")
public class ScrollNewsAction extends BaseAction {
	
	private String   infoId;
	private String   infoTitle;
	private String   infoSender;
	private String   infoContext;
	private String   infoTime;
	private int      infoReadtimes=0;
	private String   auditing="";
	private String   assessorInfo;
	private String   infoType="1";
	private Teacher  infoEditer;
	private Teacher  infoAssessor;
	private Info     info;
	
	private  String   where_search=""; //查询条件
	private  String   where_value;
	private  String   like ;
	
	
	
	//文件
	private String[] files_content;
	private String[] files_title;
	private List<File> upload_file;
	private String[] files_save_as;
	private String[] upload_fileContentType;
	private String[] upload_fileFileName;
	private String   savePath;
	private List<InfoFiles>     infoFiles;
	
	
	//分页
	private int        currentPage=1;
	private int        pageSize=5;
	private String     nextPage;
	private PageModel  pm;
	
	
	
	
	private  Set stclasses;
	
	public Set getStclasses() {
		return stclasses;
	}
	public void setStclasses(Set stclasses) {
		this.stclasses = stclasses;
	}
	public String getInfoId() {
		return infoId;
	}
	public void setInfoId(String infoId) {
		this.infoId = infoId;
	}
	public String getInfoTitle() {
		return infoTitle;
	}
	public void setInfoTitle(String infoTitle) {
		this.infoTitle = infoTitle;
	}
	public String getInfoSender() {
		return infoSender;
	}
	public void setInfoSender(String infoSender) {
		this.infoSender = infoSender;
	}
	public String getInfoContext() {
		return infoContext;
	}
	public void setInfoContext(String infoContext) {
		this.infoContext = infoContext;
	}
	public int getInfoReadtimes() {
		return infoReadtimes;
	}
	public void setInfoReadtimes(int infoReadtimes) {
		this.infoReadtimes = infoReadtimes;
	}
	public String getAuditing() {
		return auditing;
	}
	public void setAuditing(String auditing) {
		this.auditing = auditing;
	}
	public String getAssessorInfo() {
		return assessorInfo;
	}
	public void setAssessorInfo(String assessorInfo) {
		this.assessorInfo = assessorInfo;
	}
	public String getInfoType() {
		return infoType;
	}
	public void setInfoType(String infoType) {
		this.infoType = infoType;
	}
	public Teacher getInfoEditer() {
		return infoEditer;
	}
	public void setInfoEditer(Teacher infoEditer) {
		this.infoEditer = infoEditer;
	}
	public Teacher getInfoAssessor() {
		return infoAssessor;
	}
	public void setInfoAssessor(Teacher infoAssessor) {
		this.infoAssessor = infoAssessor;
	}
	public String getInfoTime() {
		return infoTime;
	}
	public void setInfoTime(String infoTime) {
		this.infoTime = infoTime;
	}
	public Info getInfo() {
		return info;
	}
	public void setInfo(Info info) {
		this.info = info;
	}
	
	public String getWhere_search() {
		return where_search;
	}
	public void setWhere_search(String whereSearch) {
		where_search = whereSearch;
	}
	public String getWhere_value() {
		return where_value;
	}
	public void setWhere_value(String where_value) {
		this.where_value = where_value;
	}
	
	public String getLike() {
		return like;
	}
	public void setLike(String like) {
		this.like = like;
	}
	public String getSearch() {
		return where_search;
	}
	public void setSearch(String where_search) {
		this.where_search = where_search;
	}

	
	
	
	public String[] getFiles_content() {
		return files_content;
	}
	public void setFiles_content(String[] filesContent) {
		files_content = filesContent;
	}
	public String[] getFiles_title() {
		return files_title;
	}
	public void setFiles_title(String[] filesTitle) {
		files_title = filesTitle;
	}
	public List<File> getUpload_file() {
		return upload_file;
	}
	public void setUpload_file(List<File> uploadFile) {
		upload_file = uploadFile;
	}
	public String[] getFiles_save_as() {
		return files_save_as;
	}
	public void setFiles_save_as(String[] filesSaveAs) {
		files_save_as = filesSaveAs;
	}
	public String[] getUpload_fileContentType() {
		return upload_fileContentType;
	}
	public void setUpload_fileContentType(String[] uploadFileContentType) {
		upload_fileContentType = uploadFileContentType;
	}
	public String[] getUpload_fileFileName() {
		return upload_fileFileName;
	}
	public void setUpload_fileFileName(String[] uploadFileFileName) {
		upload_fileFileName = uploadFileFileName;
	}
	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	public List<InfoFiles> getInfoFiles() {
		return infoFiles;
	}
	public void setInfoFiles(List<InfoFiles> infoFiles) {
		this.infoFiles = infoFiles;
	}

	
	
	
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public String getNextPage() {
		return nextPage;
	}
	public void setNextPage(String nextPage) {
		this.nextPage = nextPage;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public PageModel getPm() {
		return pm;
	}
	public void setPm(PageModel pm) {
		this.pm = pm;
	}
	
	
	
	/***
	 *  ------------Add 发布信息
	 * 
	 */
	
	public String Add() throws Exception{
   
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		
		Date  infoTimeDate = null;
		
		List<File> files=getUpload_file();
		
		List file_list = new ArrayList();
		setSavePath("");//上传路径
		boolean bl=false;
		boolean b2=false;
		
		try {
			
			infoTimeDate= sf.parse(infoTime); 
		
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	
		Info info = new Info ();
		
		info.setInfoTitle(infoTitle);
		info.setInfoContext(infoContext);
		System.out.println(infoTimeDate);
		info.setInfoTime(infoTimeDate);
		info.setInfoReadtimes(infoReadtimes);
		info.setInfoType(infoType);
		info.setAuditing("0");
		info.setAssessorInfo(assessorInfo);
		info.setInfoSender(infoSender);
		info.setTeacherByInfoAssessor(infoAssessor);
		info.setTeacherByInfoEditer(infoEditer);
		
		//先插入info表
		bl= this.getCommonService().saveObject(info);
		
		//处理文件上传

		if(files!=null)
			for(int i=0;i<files.size();i++){//控制上传部分
				String extend_name=getUpload_fileFileName()[i];
				System.out.println(extend_name);
				int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
				extend_name=extend_name.substring(extend_name_int);
							
				java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
			   String datestr = df.format(new java.util.Date()) ;
			   Random rand = new Random();//生成随机数
			   int ram = rand.nextInt(500);
				String ram_str = String.valueOf(ram);
				for(int i_i=0; ram_str.length()<6;i_i++){
					ram_str="0"+ram_str;
				}
				
				extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
				String Save_path="\\scrollNews\\image\\"+extend_name;//文件在服务器保存的相对路径
				FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
				
				FileInputStream fis=new FileInputStream(files.get(i));
				byte[] buffer=new byte[1024];
				int len=0;
				while((len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
				
				}
				
				InfoFiles afiles=new InfoFiles();
							
				
				System.out.println();
				System.out.println("----------第--"+(i+1)+"--文件--------------");
				System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
				System.out.println("--------文件名:"+files_title[i]+"--------------");
				System.out.println("--------文件说明:"+files_content[i]+"--------------");
				
				afiles.setFileUrl(Save_path);
				afiles.setFileName(files_title[i]);
				afiles.setFileContext(files_content[i]);
				afiles.setInfo(info);	
	
				//在插入附件表
				b2=this.getCommonService().saveObject(afiles);
		
		//文件上传处理结束
		
     	}
		
		if(bl)
		{
			return "AfficheAdd";
		}else{
			return  "error";
		}
		
	}
	
	/***
	 * List 后台最新所有通知列表
	 * 
	 */
      public String List() throws Exception{
    	  
    	  String hql =" from Info where  infoType=1";
    	  String[] parem = null;
    	  
    	  nextPage ="afficheList" ;
    	  if(!"".equals(where_value)&&where_value!=null){
    		  
    		  System.out.println("++++-- "+where_value);
    		  if(like.equals("like"))
    		  {
             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
    		  }else{
    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
    		  }
    		  
    	  }
    	  hql = hql +" order by infoTime desc,infoId desc";
    	  
    	  pm = new PageModel();
    	  
    	  pm=this.getCommonService().findPage(hql, parem, currentPage, pageSize, nextPage,where_search);
    	  
    	  return "AfficheList";
      }
      
	/***
	 * 
	 *   Detail  显示单条信息详细
	 */
      
	@SuppressWarnings("unchecked")
	public String Detail() throws Exception{
    	  
    	  String   hql = "from InfoFiles where infoId=?";
    	  System.out.println(infoId);
    	  String[] parem ={infoId,};
    	
    	  int id=Integer.parseInt(infoId);
    	  
    	  info =(Info)this.getCommonService().findByIntId(id, "gdut.jmxywz.been.Info");
    	  //更新阅读次数，加一
    	  info.setInfoReadtimes(info.getInfoReadtimes()+1);
    	  this.getCommonService().updateObject(info);
    	  
    	  //查找公告相应的附件
    	  infoFiles =this.getCommonService().findListByHql(hql, parem);
    	  
    	  return "AfficheDetail";
      }
	 

	
	
	/***
	 * Auditing 审核通知列表
	 * 
	 */
	  public String Auditing () throws Exception{
		  String hql ="from Info where infoType = 1";
    	  String[] parem =null;
    	  String  search_parameter = "";
          String swhere_value = where_value;
    	  System.out.println("===="+auditing);
   	    
    	  String en_where_value="";
    	  if(!"".equals(where_value)&&where_value!=null){
    		  
    	  
    		  if(like.equals("like"))
    		  {
    			
    			  System.out.println("fanhuid == "+where_value);
             	   hql =hql+"and "+where_search+" like '%"+swhere_value+"%'";
             	   System.out.println("====++++"+where_value);
             	  en_where_value=java.net.URLEncoder.encode(where_value);//URL编码转换
             	  
             	  
    		  }else{
//    			  where_value = java.net.URLDecoder.decode(where_value);

    			  hql = hql +"and "+where_search+" = '"+where_value+"'";

    	    	  en_where_value=java.net.URLEncoder.encode(where_value);//URL编码转换
    		  }
    		  
         	  System.out.println("------------++ "+search_parameter);
    	  }

    	  
    	  if(("0").equals(auditing))
    	  {
    	     hql =hql +"and auditing="+ auditing +" order by  auditing  asc ,infoTime desc,infoId desc  ";
    	     nextPage ="afficheAuditing?auditing=0&" ;
    	     
    	     
    	  }
    	  else {
    		  
    		  nextPage ="afficheAuditing" ;
    	
    		  hql =hql + " order by auditing asc ,infoTime desc, infoId desc ";
    		  
    	  }
    	  System.out.println("=============  "+where_value);
    	  System.out.println("============= "+where_search);
    	  System.out.println("------------- " +like);
    	  

		   search_parameter="&where_search="+ where_search+"&like="+like+"&where_value="+en_where_value;
    	  
		   System.out.println("---"+search_parameter);
		   System.out.println("---"+hql);
		   //    	  List l = this.getCommonService().findListByHql(hql, parem);
//    	  
//    	  Iterator iter = l.iterator();
//    	  if(!iter.hasNext()){
//    		  System.out.println("为空————————");
//    	  }
//    	  while (iter.hasNext()){
//    		  
//    		  Info inf = (Info)iter.next();
//    		  System.out.println(inf.getInfoTitle());
//    		  System.out.println(inf.getInfoSender());
//    		  
//    	  }
    	  pm = new PageModel();
    	  
    	  System.out.println("hql222---"+hql);
    	  pm=this.getCommonService().findPage(hql,parem, currentPage, pageSize, nextPage,search_parameter);
    	   System.out.println("---------------------"+pm.getTotalCount());
		  
		  return "AfficheAuditing";
	  }
	
	
	/***
	 * Inside  院内公布
	 * 
	 */
	  public String Inside() throws Exception{
		  
		  String   hql = "from InfoFiles where infoId=?";
    	  System.out.println(infoId);
    	  String[] parem ={infoId,};
    	
    	  int id=Integer.parseInt(infoId);
    	  
    	  info =(Info)this.getCommonService().findByIntId(id, "gdut.jmxywz.been.Info");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
    	  info.setAuditing("1");
    	  this.getCommonService().updateObject(info);
    	  
		  
		  
		  return "AfficheInside";
	  }
	  
	
    /***
     *   Outside 对外公布
     */
    public String Outside() throws Exception{
		  
		  String   hql = "from InfoFiles where infoId=?";
    	  System.out.println(infoId);
    	  String[] parem ={infoId,};
    	
    	  int id=Integer.parseInt(infoId);
    	  
    	  info =(Info)this.getCommonService().findByIntId(id, "gdut.jmxywz.been.Info");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
    	  info.setAuditing("2");
    	  this.getCommonService().updateObject(info);
    	  
		  return "AfficheOutside";
	  }
	
	/***
	 * 
	 *    Notallow 不通过
	 */
    public String Notallow() throws Exception{
		  
      String   hql = "from InfoFiles where infoId=?";
  	  System.out.println(infoId);
  	  String[] parem ={infoId,};
  	
  	  int id=Integer.parseInt(infoId);
  	  
  	  info =(Info)this.getCommonService().findByIntId(id, "gdut.jmxywz.been.Info");
  	  //将信息审核为对院内公布 ，即设置auditing为1；
  	  info.setAuditing("3");
  	  this.getCommonService().updateObject(info);
  	  
		  return "AfficheNotallow";
	  }
	
    
    
	
	/***
	 * 
	 *    Rollback 未审核
	 */
    public String Rollback() throws Exception{
		  
          String   hql = "from InfoFiles where infoId=?";
    	  System.out.println(infoId);
    	  String[] parem ={infoId,};
    	
    	  int id=Integer.parseInt(infoId);
    	  
    	  info =(Info)this.getCommonService().findByIntId(id, "gdut.jmxywz.been.Info");
    	  //将信息审核为对院内公布 ，即设置auditing为1；
    	  info.setAuditing("0");
    	  this.getCommonService().updateObject(info);
    	  
  		  return "AfficheRollback";
  	  }
    
	
	/***
	 * 
	 *  ModifyShow 修改时显示单体记录
	 */
    
     @SuppressWarnings("unchecked")
	public String ModifyShow() throws Exception{
    	 
		      String   hql = "from InfoFiles where infoId=?";
		   	  System.out.println(infoId);
		   	  String[] parem ={infoId,};
		   	
		   	  int id=Integer.parseInt(infoId);
		   	  
		   	  //查找相应的通知
		   	  System.out.println(infoId);
		   	  info =(Info)this.getCommonService().findByIntId(id, "gdut.jmxywz.been.Info");
		       
		   	  System.out.println("-------------"+info.getInfoTitle());
		  
		   	  
		   	  //查找公告相应的附件
		   	 infoFiles = this.getCommonService().findListByHql(hql, parem);
		   	 Iterator iter = infoFiles.iterator();
		   	 while (iter.hasNext()){
		   		 InfoFiles in = (InfoFiles)iter.next();
		   		 
		   		 System.out.println("infoFiles ----"+ in.getFileName());
		   	 }
		   	 
    	 return "AfficheModifyShow";
     }
	
    
    
	
	/***
	 * 
	 *   Modify 修改 
	 *   
	 */
	
    public  String Modify() throws Exception {
    	
    	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		
		Date  infoTimeDate = null;
		
		List<File> files=getUpload_file();
		
		List file_list = new ArrayList();
		setSavePath("");//上传路径
		boolean bl=false;
		boolean b2=false;
		
		try {
			
			infoTimeDate= sf.parse(infoTime); 
		
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	
		
		System.out.println("---Modify---"+infoTitle);
		int id = Integer.parseInt(infoId);
		System.out.println("---ModifyId---"+id);
		Info info = (Info)this.getCommonService().findByIntId(id, "gdut.jmxywz.been.Info");
		
		info.setInfoTitle(infoTitle);
		info.setInfoContext(infoContext);
		System.out.println(infoTimeDate);
		info.setInfoTime(infoTimeDate);
		info.setInfoReadtimes(infoReadtimes);
		info.setInfoType(infoType);
		info.setAuditing(auditing);
		info.setAssessorInfo(assessorInfo);
		info.setInfoSender(infoSender);
		info.setTeacherByInfoAssessor(infoAssessor);
		info.setTeacherByInfoEditer(infoEditer);
		
		//先插入info表
		
		bl= this.getCommonService().updateObject(info);
		
		//处理文件上传
	     System.out.println("==="+files_content);  
	     System.out.println("=="+files_title);
		if(files!=null)
			for(int i=0;i<files.size();i++){//控制上传部分
				String extend_name=getUpload_fileFileName()[i];
				System.out.println(extend_name);
				int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
				extend_name=extend_name.substring(extend_name_int);
							
				java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
			   String datestr = df.format(new java.util.Date()) ;
			   Random rand = new Random();//生成随机数
			   int ram = rand.nextInt(500);
				String ram_str = String.valueOf(ram);
				for(int i_i=0; ram_str.length()<6;i_i++){
					ram_str="0"+ram_str;
				}
				
				extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
				String Save_path="\\scrollNews\\image\\"+extend_name;//文件在服务器保存的相对路径
				FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
				
				FileInputStream fis=new FileInputStream(files.get(i));
				byte[] buffer=new byte[1024];
				int len=0;
				while((len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
				
				}
				
				InfoFiles afiles=new InfoFiles();
				
				System.out.println();
				System.out.println("----------第--"+(i+1)+"--文件--------------");
				System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
				System.out.println("--------文件名:"+files_title[i]+"--------------");
				System.out.println("--------文件说明:"+files_content[i]+"--------------");
				
				afiles.setFileUrl(Save_path);
				afiles.setFileName(files_title[i]);
				afiles.setFileContext(files_content[i]);
				afiles.setInfo(info);	
	
				//在插入附件表
				b2=this.getCommonService().saveObject(afiles);
		
		//文件上传处理结束
		
     	}
		
		if(bl)
		{
			return "AfficheModify";
		}else{
			return  "error";
		}
    	
    }
	
	
	/***
	 *   Delete 删除
	 */
    public String Delete() throws Exception{
		  
    	  System.out.println(infoId);
    	  int id=Integer.parseInt(infoId);
    	  
    	  Info info = new Info();
          
    	  info =(Info) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.Info");
         System.out.println("=================");
    	  boolean b =this.getCommonService().deleteObject(info);
    	  if(b){
    		  return "AfficheDelete";
    	  }else {
    		  return "error";
    	  }
    	  //将信息审核为对院内公布 ，即设置auditing为1；
    	  
  	
  	  }
    
	    
	 
	 
	

	
	
	
}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
